using IrisGenerator.Common.Tools;
using IrisGenerator.Common.Core;
using IrisGenerator.Framework.RuleEngine;
using IrisGenerator.Framework.Events;
using System.ComponentModel;

namespace IrisGenerator.Common.DataTypes
{

    public partial class Metatype : BindableObject<Metatype>, IRuleable<Metatype>, INotifyPropertyChanged
    {

        private System.Nullable<System.Guid> _metId;

        private string _metCode;

        private string _metDescription;

        //private ColumnCollection _columnCollection;

        private MetatypeEquivalencesCollection _metatypeEquivalencesCollection;

        //private RequestCollection _requestCollection;

        //private RequestParameterCollection _requestParameterCollection;

        public virtual System.Nullable<System.Guid> MetId
        {
            get
            {
                return _metId;
            }
            set
            {
                
                _metId = value;
                this.OnPropertyChanged("MetId");
            }
        }

        public virtual string MetCode
        {
            get
            {
                return _metCode;
            }
            set
            {
                _metCode = value;
                this.OnPropertyChanged("MetCode");
            }
        }

        public virtual string MetDescription
        {
            get
            {
                return _metDescription;
            }
            set
            {
                _metDescription = value;
                this.OnPropertyChanged("MetDescription");
            }
        }

        //public virtual ColumnCollection ColumnCollection
        //{
        //    get
        //    {
        //        if ((this._columnCollection == null))
        //        {
        //            _columnCollection = Column.Select_COLUMN_By_MET_ID(this.MetId);
        //        }
        //        return this._columnCollection;
        //    }
        //}


        
        public virtual MetatypeEquivalencesCollection MetatypeEquivalencesCollection
        {
            get
            {
                if ((this._metatypeEquivalencesCollection == null))
                {
                    _metatypeEquivalencesCollection = MetatypeEquivalences.Select_METATYPE_EQUIVALENCES_By_MET_ID(this.MetId);
                }
                return this._metatypeEquivalencesCollection;
            }
        }

        //public virtual RequestCollection RequestCollection
        //{
        //    get
        //    {
        //        if ((this._requestCollection == null))
        //        {
        //            _requestCollection = Request.Select_REQUEST_By_MET_ID(this.MetId);
        //        }
        //        return this._requestCollection;
        //    }
        //}

        //public virtual RequestParameterCollection RequestParameterCollection
        //{
        //    get
        //    {
        //        if ((this._requestParameterCollection == null))
        //        {
        //            _requestParameterCollection = RequestParameter.Select_REQUEST_PARAMETER_By_MET_ID(this.MetId);
        //        }
        //        return this._requestParameterCollection;
        //    }
        //}

        private void Clean()
        {
            this.MetId = null;
            this.MetCode = string.Empty;
            this.MetDescription = string.Empty;
            //this._columnCollection = null;
            this._metatypeEquivalencesCollection = null;
            //this._requestCollection = null;
            //this._requestParameterCollection = null;
        }

        private void Fill(System.Data.DataRow dr)
        {
            this.Clean();
            if ((dr["MET_ID"] != System.DBNull.Value))
            {
                this.MetId = ((System.Nullable<System.Guid>)(dr["MET_ID"]));
            }
            if ((dr["MET_CODE"] != System.DBNull.Value))
            {
                this.MetCode = ((string)(dr["MET_CODE"]));
            }
            if ((dr["MET_DESCRIPTION"] != System.DBNull.Value))
            {
                this.MetDescription = ((string)(dr["MET_DESCRIPTION"]));
            }
        }

        public static MetatypeCollection GetAll()
        {
            IrisGenerator.Dal.Metatype dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Metatype();
                System.Data.DataSet ds = dbo.METATYPE_Select_All();
                MetatypeCollection collection = new MetatypeCollection();
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    for (int i = 0; (i < ds.Tables[0].Rows.Count); i = (i + 1))
                    {
                        Metatype obj = new Metatype();
                        obj.Fill(ds.Tables[0].Rows[i]);
                        if ((obj != null))
                        {
                            collection.Add(obj);
                        }
                    }
                }
                return collection;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public static Metatype Load(System.Nullable<System.Guid> MET_ID)
        {
            IrisGenerator.Dal.Metatype dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Metatype();
                System.Data.DataSet ds = dbo.METATYPE_Select_One(MET_ID);
                Metatype obj = null;
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    if ((ds.Tables[0].Rows.Count > 0))
                    {
                        obj = new Metatype();
                        obj.Fill(ds.Tables[0].Rows[0]);
                    }
                }
                return obj;
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public virtual void Load()
        {
            IrisGenerator.Dal.Metatype dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Metatype();
                System.Data.DataSet ds = dbo.METATYPE_Select_One(this.MetId);
                if (GlobalTools.IsSafeDataSet(ds))
                {
                    if ((ds.Tables[0].Rows.Count > 0))
                    {
                        this.Fill(ds.Tables[0].Rows[0]);
                    }
                }
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public override void Insert()
        {
            IrisGenerator.Dal.Metatype dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Metatype();
                this.OnInserting(new BaseEventArgs<Metatype>(this,false));
                this.MetId=dbo.METATYPE_Insert(this.MetCode, this.MetDescription);
                this.OnInserted(new BaseEventArgs<Metatype>(this, false));
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }

        public override bool Delete(bool fireRuleMessages)
        {
            RuleErrorCollection errors=null;
            if (this.ObjectRules.CanDelete(ref errors))
            {
                IrisGenerator.Dal.Metatype dbo = null;
                try
                {
                    dbo = new IrisGenerator.Dal.Metatype();
                    BaseEventArgs<Metatype> e = new BaseEventArgs<Metatype>(this, fireRuleMessages);
                    this.OnDeleting(e);
                    if (!e.Cancel)
                    {
                        dbo.METATYPE_Delete(this.MetId);
                        return true;
                    }
                    else
                        return false;
                }
                catch (System.Exception ex)
                {
                    throw;
                }
                finally
                {
                    if ((dbo != null))
                    {
                        dbo.Dispose();
                    }
                }
            }
            else
            {
                //TODO: Traitement des erreurs...
                return false;
            }
        }

        public override void Update()
        {
            IrisGenerator.Dal.Metatype dbo = null;
            try
            {
                dbo = new IrisGenerator.Dal.Metatype();
                dbo.METATYPE_Update(this.MetId, this.MetCode, this.MetDescription);
            }
            catch (System.Exception)
            {
                throw;
            }
            finally
            {
                if ((dbo != null))
                {
                    dbo.Dispose();
                }
            }
        }
    }
}
